﻿using CodeApi.Context;
using CodeApi.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Threading.Tasks;

namespace CodeApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class FileUpload : ControllerBase
    {
        public static IWebHostEnvironment _environment;
        public IndexContext indexContext { get; set; }
        public PictureBenContext pictureBenContext;

        public FileUpload(IWebHostEnvironment environment, IndexContext indexContext, PictureBenContext pictureBenContext)
        {
            _environment = environment;
            this.indexContext = indexContext;
            this.pictureBenContext = pictureBenContext;
        }

        [HttpPost]
        public async Task<ActionResult> PostFile([FromForm] FilePostModel objFile)
        {
            try
            {
                if (objFile.files.Length > 0)
                {
                    string path = _environment.WebRootPath + "\\MarkDown\\";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    using (FileStream filesStream = System.IO.File.Create(path + objFile.files.FileName))
                    {
                        PictureBenController pictureBenController = new PictureBenController(_environment, pictureBenContext);
                        String path1 = await pictureBenController.PostPicture(new ImagePostModel() { File = objFile.Img1 });
                        String path2 = await pictureBenController.PostPicture(new ImagePostModel() { File = objFile.Img2 });
                        String path3 = await pictureBenController.PostPicture(new ImagePostModel() { File = objFile.Img3 });
                        objFile.files.CopyTo(filesStream);
                        filesStream.Flush();
                        indexContext.articles.Add(new ArticleModel()
                        {
                            ArticleTitle = objFile.ArticleTitle,
                            ArticleContent = objFile.ArticleContent,
                            UID = objFile.UID,
                            Img1 = $"https://{Request.Host}{path1}",
                            Img2 = $"https://{Request.Host}{path2}",
                            Img3 = $"https://{Request.Host}{path3}",
                            Kind = objFile.Kind,
                            Number = objFile.Number,
                            CreateTime = DateTime.Now,
                            Path = "\\MarkDown\\" + objFile.Path
                        });
                        await indexContext.SaveChangesAsync();
                        return new ObjectResult(new { filePath = objFile.files.FileName });
                    }
                }
                else
                {
                    return new ObjectResult(new { filePath = "null" });
                }
            }
            catch (Exception ex)
            {
                return new ObjectResult(new { error = ex.Message });
            }
        }
    }
}